﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Veteran_cms.Models;
using THS.HtmlHelpers;
using System.IO;
namespace Veteran_cms.Controllers
{
    public class ManageMenuController : BaseController
    {
        Veterandb ctx = new Veterandb();
        //
        // GET: /ManageAbout/
        const int PageSize = 6;
        public ActionResult IndexMenu(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            IList<menu> mn = ctx.menus.OrderBy(p=>p.menu_id).ToList();
            model.MenuList = mn.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.MenuInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = mn.Count() };
            return View(model);
            
        }
        public ActionResult Paging(int page = 1)
        {
            var model = new HomeAdminListViewModel();
            IList<menu> mn = ctx.menus.ToList();
            model.MenuList = mn.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.MenuInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = mn.Count() };
            return PartialView("UCMenuList",model);

        }
      
        public ActionResult Details(int id)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var mn = (from menu in ctx.menus where menu.menu_id == id select menu).SingleOrDefault();
            return View(mn);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Details(int id,menu model)
        {
            try
            {
                // TODO: Add update logic here
                // if (!IsAuthenticateAdmin())
                //   return RedirectToAction("LogOn", "AdminHome");
                menu mn = ctx.menus.Where(p => p.menu_id == id).SingleOrDefault();
                UpdateModel(mn);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã cập nhật!";
                return View(mn);
                
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "AdminHome");
                menu cust = ctx.menus.Where(p => p.menu_id == id).SingleOrDefault();
                ctx.menus.DeleteObject(cust);
                ctx.SaveChanges();
                return RedirectToAction("IndexMenu");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FunRingManagement/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "menu_id")]menu mn)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            mn.created_date = DateTime.Now;
           
                ctx.AddTomenus(mn);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã tạo thành công!";
                return RedirectToAction("IndexMenu");
            
        }

        #region --MenuItem-
        //menu item
        public ActionResult CreateMenuItem(int menu_id)
        {
            InitParentMenu("");
            var model = new menu_item();
            model.menu_id = menu_id;
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateMenuItem([Bind(Exclude = "menu_item_id")]menu_item mnItem)
        {
            ctx.AddTomenu_item(mnItem);
            ctx.SaveChanges();
            ViewData["Msg"] = "Đã tạo thành công!";
            InitParentMenu("");
            var model = new menu_item();
            model.menu_id = mnItem.menu_id;
            return View(model);
        }
        public ActionResult UpdateToMenuItem(int parent_id, int id, String label,String link)
        {
            var mnItem = ctx.menu_item.Where(p => p.menu_item_id== id).SingleOrDefault();
            mnItem.parent_id = parent_id;
            mnItem.label = label;
            mnItem.link = link;
            ctx.SaveChanges();
            ViewData["Msg"]="Cập nhật thành công!";
            return RedirectToAction("Details", "ManageMenu", new { id = mnItem.menu_id });
        }
        public ActionResult DeleteMenuItem(int id)
        {
            
            List<menu_item> lst = ctx.menu_item.Where(p => p.parent_id == id).ToList();
            foreach (var item in lst)
            {
                ctx.DeleteObject(item);
                ctx.SaveChanges();
            }
            menu_item mnItem = ctx.menu_item.Where(p => p.menu_item_id == id).SingleOrDefault();
            int menu_id = mnItem.menu_id;
            ctx.DeleteObject(mnItem);
            ctx.SaveChanges();
            return RedirectToAction("Details", new {id=menu_id });
        }
        private void InitParentMenu(object selected)
        {
            menu_item mn = new menu_item();
            List<menu_item> ctg = ctx.menu_item.ToList();
            ctg.Insert(0, mn);
            ViewData["parent_id"] = new SelectList(ctg.AsEnumerable<menu_item>(), "menu_item_id", "label", selected);
        }
        public bool CheckExistParent(int id)
        {
            List<menu_item> lst=ctx.menu_item.Where(p => p.parent_id == id).ToList();
            if (lst.Count() > 0)
                return true;
            return false;
        }
        #endregion
    }
}
